export function creatElement(element?: Element | string | null | undefined): HTMLElement {
    let ans;
    if (element instanceof HTMLElement) {
        ans = element;
    } else if (typeof element === "string") {
        ans = document.querySelector(element);
    } else {
        ans = document.createElement("div");
    }
    ans.style.position = "absolute";
    ans.style.margin = "0";
    return ans;
}


export function hash(str: string) {
    var hash = 5381,
        i = str.length;
    while (i) {
        hash = (hash * 33) ^ str.charCodeAt(--i);
    }
    return hash >>> 0;
}

let colors = [
    "#D73C2C", "#870000",
    "#ca2c68", "#fa5c98",
    "#7e349d", "#3e005d",
    "#0067b0", "#22A7F0",
    "#009b90", "#106b60",
    "#106b60", "#006c11",
    "#f9b32f", "#e67e22",
    "#5c6a79", "#2c3a49"];

export function getColorFromString(string: string) {
    return hash(string) % 5;
}

export function getWebSocketAddress() {
    let address = "";
    if (location.protocol === "https:")
        address += "wss:";
    else
        address += "ws:";
    address += "//";
    address += location.hostname;
    address += location.port ? ":" + location.port : "";
    let array = location.pathname.split("/");
    array.pop();
    address += array.join("/");
    address += "/websocket.do";
    return address;
}

let LabColors = ["badge-primary", "badge-success", "badge-info", "badge-warning", "badge-danger"]

// badge-dark badge-secondary badge-light

function getLabColorByName(name: string): string {
    let i = hash(name) % LabColors.length;
    return LabColors[i];
}


export function createBadge(name: string, color?: string): HTMLElement {
    let badge = document.createElement("span");
    badge.innerText = name;
    badge.classList.add("badge");
    if (color) {
        badge.classList.add(color);
    } else {
        badge.classList.add(getLabColorByName(name));
    }
    return badge;
}
import { DataSegment } from '../datastructure/DataSegment'
import { DataProcessor } from '../datastructure/DataProcessor'



export function textEncode(str: string) {
    var utf8 = unescape(encodeURIComponent(str));
    var result = new Uint8Array(utf8.length);
    for (var i = 0; i < utf8.length; i++) {
        result[i] = utf8.charCodeAt(i);
    }
    return result;
}

export async function sha256(message: string) {
    const msgBuffer = textEncode(message);
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
    return hashHex;
}

export let setverPublicKey = "-----BEGIN PUBLIC KEY-----\n" +
    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJ4uojnuFlX3dXn7L4UFs7zvJ2\n" +
    "m7BrTzteWHjKCbzILHb5DO9+WjOsenFKKfgxIzjUiE2tq4zaUxLKLCTc2i5XaGY+\n" +
    "gJ7sYe5dwoa8n5jYDkR+K2f8LA8H/HE7pelG+ddlXSBXS4rnjVAkpfIu4CiXOURo\n" +
    "5iQD6a/DUc55xDUAQQIDAQAB\n" +
    "-----END PUBLIC KEY-----";



/*
 注册:
 目标 防止重放攻击


登录
 1. 打开链接 发送自己的用户名
 2. 获取公钥加密的随机数
 3. 解密再加密重发

 登录2:
 1. 获取随机数
 2. gpg(发送用户名和hash后的密码)
 3. 协商密钥
*/
